# Contributor: Jakub Panek <me@panekj.dev>
# Maintainer: Matthias Ahouansou <matthias@ahouansou.cz>
pkgname=wezterm
pkgver=20240203.110809
_pkgver="${pkgver/./-}-5046fc22"
pkgrel=2
pkgdesc="GPU-accelerated cross-platform terminal emulator and multiplexer written in Rust"
url="https://wezfurlong.org/wezterm/"
# s390x blocked by nix crate
arch="all !s390x"
license="MIT"
# See remove-bundled-fonts.patch for more info about fonts.
_depends_fonts="
	font-jetbrains-mono-vf
	font-noto-emoji
	font-roboto
	"
makedepends="
	$_depends_fonts
	cargo
	cargo-auditable
	fontconfig-dev
	freetype-dev
	harfbuzz-dev
	libgit2-dev
	libpng-dev
	libssh2-dev
	libx11-dev
	libxkbcommon-dev
	lua5.4-dev
	ncurses
	openssl-dev>3
	python3
	wayland-dev
	xcb-imdkit-dev
	xcb-util-dev
	xcb-util-image-dev
	xcb-util-keysyms-dev
	xcb-util-wm-dev
	zlib-dev
	zstd-dev
	"
checkdepends="openssh"
install="$pkgname.post-install $pkgname.post-upgrade"
subpackages="
	$pkgname-common
	$pkgname-mux-server:_mux_server
	$pkgname-fonts::noarch
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="
	https://github.com/wez/wezterm/releases/download/$_pkgver/wezterm-$_pkgver-src.tar.gz
	link-against-system-libs.patch
	rust1.78-from_raw_parts.patch
	"
builddir="$srcdir/wezterm-$_pkgver"
options="net"

export LIBSSH2_SYS_USE_PKG_CONFIG=1  # use system libssh2

prepare() {
	cargo fetch --target="$CTARGET" --locked

	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libs
	mkdir -p .cargo
	cat >> .cargo/config <<-EOF
		[target.$target]
		git2 = { rustc-link-lib = ["git2"] }
	EOF

	cargo fetch --target="$CTARGET"  # regenerate Cargo.lock after patching

	# dont print malformed sshd config, this can possibly fail on builders but not on CI
	sed 's|std::fs::write|//std::fs::write|g' -i "$builddir"/wezterm-ssh/tests/sshd.rs
}

build() {
	cargo auditable build \
		--release \
		--frozen \
		--no-default-features \
		--features wayland,distro-defaults \
		--bin wezterm \
		--bin wezterm-gui \
		--bin wezterm-mux-server \
		--bin strip-ansi-escapes

	# Regenerate shell completions (to be up-to-date).
	local sh; for sh in bash fish zsh; do
		./target/release/wezterm shell-completion --shell $sh > assets/shell-completion/$sh
	done
}

check() {
	# Test ligatures_jetbrains is somehow broken; it's *not* related to our
	# patches, but probably to fontconfig or freetype version.
	cargo test --frozen -- \
		--skip shapecache::test::ligatures_jetbrains

	# required to unstuck CI
	killall -q sshd || true
}

package() {
	depends="$pkgname-common=$pkgver-r$pkgrel"
	local appid="org.wezfurlong.wezterm"

	cd target/release
	install -Dm755 -t "$pkgdir"/usr/bin/ \
		wezterm \
		wezterm-gui \
		wezterm-mux-server \
		strip-ansi-escapes

	cd "$builddir"

	install -Dm644 assets/wezterm.desktop "$pkgdir"/usr/share/applications/$appid.desktop
	install -Dm644 assets/wezterm.appdata.xml "$pkgdir"/usr/share/metainfo/$appid.appdata.xml
	install -Dm644 LICENSE.md -t "$pkgdir"/usr/share/licences/$pkgname/
	install -Dm644 assets/shell-integration/wezterm.sh -t "$pkgdir"/etc/profile.d/

	install -Dm644 assets/icon/terminal.png "$pkgdir"/usr/share/icons/hicolor/128x128/apps/$appid.png
	install -Dm644 assets/icon/wezterm-icon.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/$appid.svg

	install -Dm644 assets/shell-completion/bash "$pkgdir"/usr/share/bash-completion/completions/wezterm
	install -Dm644 assets/shell-completion/fish "$pkgdir"/usr/share/fish/vendor_completions.d/wezterm.fish
	install -Dm644 assets/shell-completion/zsh "$pkgdir"/usr/share/zsh/site-functions/_wezterm
}

# NOTE: This package contains programs that don't depend on Gtk, X11 etc.
#  It may be installed on a remote server.
common() {
	pkgdesc="Common files for WezTerm GUI and multiplexer server"
	depends="ncurses-terminfo"

	amove etc/profile.d
	amove usr/bin/strip-ansi-escapes
	amove usr/bin/wezterm
}

_mux_server() {
	pkgdesc="WezTerm multiplexer server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove usr/bin/wezterm-mux-server
}

fonts() {
	pkgdesc="Meta package for fonts bundled in Wezterm"
	depends="$_depends_fonts"

	mkdir -p "$subpkgdir"
}

sha512sums="
2413e1537ed4cf699f0754d76f35cd679591e4e215e782fab61f5bd7c4615e3916398e16709445406ef241e3fb721111daed917c546abb1f5130109b40bb2774  wezterm-20240203-110809-5046fc22-src.tar.gz
d4c13f079dd0348d60eed577a0467cf4da9b202c5c959902b0626019c95d945d2a91023bc6844c19d5a3aecaa8d0de007966a6a2113549fe5328c3c8de79381f  link-against-system-libs.patch
a13edbf756ef697ecc640103589d686043f4790b36ac0867a777c47cae1b6cd7482eca229d514fb6feb61e5b274c564e304eacad90001dff82e3dc01f20a3ffa  rust1.78-from_raw_parts.patch
"
